<template>
  <my-container
    :left-aside-width="'45%'"
    :show-left-aside="true"
    :resizable="true"
  >
    <template #left-aside>
      <my-dictionary-type @change="onTypeChange" />
    </template>
    <my-container v-loading="pageLoading">
      <!--查询-->
      <template #header>
        <el-form class="ad-form-query" :inline="true" :model="filter" @submit.native.prevent>
          <el-form-item>
            <el-input
              v-model="filter.name"
              placeholder="名称"
              clearable
              @keyup.enter.native="onSearch"
            >
              <template #prefix>
                <i class="el-input__icon el-icon-search" />
              </template>
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSearch">查询</el-button>
          </el-form-item>
          <el-form-item v-if="checkPermission(['api:admin:dictionary:add'])">
            <el-button type="primary" @click="onAdd">新增</el-button>
          </el-form-item>
          <el-form-item v-if="checkPermission(['api:admin:dictionary:batchsoftdelete'])">
            <my-confirm-button
              :disabled="sels.length === 0"
              :type="'delete'"
              :placement="'bottom-end'"
              :loading="deleteLoading"
              :validate="batchDeleteValidate"
              style="margin-left: 0px;"
              @click="onBatchDelete"
            >
              <template #content>
                <p>确定要批量删除吗？</p>
              </template>
              批量删除
            </my-confirm-button>
          </el-form-item>
        </el-form>
      </template>

      <!--列表-->
      <el-table
        v-loading="listLoading"
        :data="dataList"
        highlight-current-row
        height="'100%'"
        style="width: 100%;height:100%;"
        @selection-change="onSelectionChange"
      >
        <template #empty>
          <el-empty :image-size="100" />
        </template>
        <el-table-column type="selection" width="50" />
        <el-table-column prop="name" label="名称" width />
        <el-table-column prop="code" label="编码" width />
        <el-table-column prop="value" label="值" width />
        <el-table-column prop="enabled" label="状态" width>
          <template #default="{row}">
            <el-tag :type="row.enabled ? 'success' : 'danger'" disable-transitions>
              {{ row.enabled ? '启用' : '禁用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column v-if="checkPermission(['api:admin:dictionary:update','api:admin:dictionary:softdelete'])" label="操作" width="180">
          <template #default="{ $index, row }">
            <el-button v-if="checkPermission(['api:admin:dictionary:update'])" @click="onEdit($index, row)">编辑</el-button>
            <my-confirm-button
              v-if="checkPermission(['api:admin:dictionary:softdelete'])"
              type="delete"
              :loading="row._loading"
              :validate="deleteValidate"
              :validate-data="row"
              @click="onDelete($index, row)"
            />
          </template>
        </el-table-column>
      </el-table>

      <!--分页-->
      <template #footer>
        <my-pagination
          ref="pager"
          :total="total"
          :checked-count="sels.length"
          :auto-layout="false"
          layout="simpleJumper"
          @get-page="getDataList"
        />
      </template>

      <!--新增窗口-->
      <my-window
        v-if="checkPermission(['api:admin:dictionary:add'])"
        title="新增数据小类"
        drawer
        embed
        :visible.sync="addFormVisible"
        @close="closeAddForm"
      >
        <el-form
          ref="addForm"
          :model="addForm"
          :rules="addFormRules"
          label-width="80px"
          :inline="false"
        >
          <el-row>
            <el-col :xs="24" :sm="12">
              <el-form-item label="名称" prop="name">
                <el-input v-model="addForm.name" auto-complete="off" />
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12">
              <el-form-item label="编码" prop="code">
                <el-input v-model="addForm.code" auto-complete="off" />
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12">
              <el-form-item label="值" prop="value">
                <el-input v-model="addForm.value" auto-complete="off" />
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12">
              <el-form-item label="启用" prop="enabled">
                <el-switch v-model="addForm.enabled" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :xs="24">
              <el-form-item label="说明" prop="description">
                <el-input v-model="addForm.description" type="textarea" :rows="2" auto-complete="off" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <template #footer>
          <el-button @click.native="addFormVisible = false">取消</el-button>
          <my-confirm-button type="submit" :validate="addFormValidate" :loading="addLoading" @click="onAddSubmit" />
        </template>
      </my-window>

      <!--编辑窗口-->
      <my-window
        v-if="checkPermission(['api:admin:dictionary:update'])"
        title="编辑数据小类"
        drawer
        embed
        :visible.sync="editFormVisible"
        @close="closeEditForm"
      >
        <el-form
          ref="editForm"
          :model="editForm"
          :rules="editFormRules"
          label-width="80px"
          :inline="false"
        >
          <el-row>
            <el-col :xs="24" :sm="12">
              <el-form-item label="名称" prop="name">
                <el-input v-model="editForm.name" auto-complete="off" />
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12">
              <el-form-item label="编码" prop="code">
                <el-input v-model="editForm.code" auto-complete="off" />
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12">
              <el-form-item label="值" prop="value">
                <el-input v-model="editForm.value" auto-complete="off" />
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12">
              <el-form-item label="启用" prop="enabled">
                <el-switch v-model="editForm.enabled" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :xs="24">
              <el-form-item label="说明" prop="description">
                <el-input v-model="editForm.description" type="textarea" :rows="2" auto-complete="off" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <template #footer>
          <el-button @click.native="editFormVisible = false">取消</el-button>
          <my-confirm-button type="submit" :validate="editFormValidate" :loading="editLoading" @click="onEditSubmit" />
        </template>
      </my-window>
    </my-container>
  </my-container>

</template>

<script>
import dictionaryApi from '@/api/admin/dictionary'
import MyConfirmButton from '@/components/my-confirm-button'
import MyWindow from '@/components/my-window'
import MyDictionaryType from './type'

/**
 * 数据字典
 */
export default {
  name: 'Dictionary',
  _sync: {
    disabled: false,
    title: '数据字典',
    desc: '',
    cache: true
  },
  components: { MyConfirmButton, MyWindow, MyDictionaryType },
  data() {
    return {
      filter: {
        name: '',
        dictionaryTypeId: this.dictionaryTypeId
      },
      dataList: [],
      total: 0,
      sels: [], // 列表选中列
      listLoading: false,
      pageLoading: false,

      addFormVisible: false, // 新增界面是否显示
      addLoading: false,
      addFormRules: {
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        code: [{ required: true, message: '请输入编码', trigger: 'blur' }]
      },
      // 新增界面数据
      addForm: {
        name: '',
        description: '',
        enabled: true,
        dictionaryTypeId: this.dictionaryTypeId
      },
      addFormRef: null,

      editFormVisible: false, // 编辑界面是否显示
      editLoading: false,
      editFormRules: {
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        code: [{ required: true, message: '请输入编码', trigger: 'blur' }]
      },
      // 编辑界面数据
      editForm: {
        id: 0,
        name: '',
        description: '',
        enabled: ''
      },
      editFormRef: null,

      deleteLoading: false,
      selectPermissionVisible: false,
      currentRow: null
    }
  },
  methods: {
    onTypeChange(row) {
      if (row?.id > 0) {
        this.filter.dictionaryTypeId = row.id
        this.addForm.dictionaryTypeId = row.id
        this.getDataList()
      }
    },
    onSearch() {
      this.$refs.pager.setPage(1)
      this.getDataList()
    },
    // 获取列表
    async getDataList() {
      var pager = this.$refs.pager.getPager()
      const params = {
        ...pager,
        filter: this.filter
      }
      this.listLoading = true
      const res = await dictionaryApi.getPage(params)
      this.listLoading = false

      if (!res?.success) {
        return
      }

      this.total = res.data.total
      const data = res.data.list
      data.forEach(d => {
        d._loading = false
      })
      this.dataList = data
    },
    // 显示新增界面
    onAdd() {
      this.addFormVisible = true
    },
    closeAddForm() {
      this.$refs.addForm.resetFields()
    },
    // 新增验证
    addFormValidate: function() {
      let isValid = false
      this.$refs.addForm.validate(valid => {
        isValid = valid
      })
      return isValid
    },
    // 新增
    async onAddSubmit() {
      this.addLoading = true
      const para = _.cloneDeep(this.addForm)

      const res = await dictionaryApi.add(para)
      this.addLoading = false

      if (!res?.success) {
        return
      }

      this.$message({
        message: this.$t('admin.addOk'),
        type: 'success'
      })
      this.$refs['addForm'].resetFields()
      this.addFormVisible = false
      this.getDataList()
    },
    // 显示编辑界面
    async onEdit(index, row) {
      this.pageLoading = true
      const res = await dictionaryApi.get({ id: row.id })
      this.pageLoading = false
      if (res && res.success) {
        const data = res.data
        this.editForm = data
        this.editFormVisible = true
      }
    },
    closeEditForm() {
      this.$refs.editForm.resetFields()
    },
    // 编辑验证
    editFormValidate: function() {
      let isValid = false
      this.$refs.editForm.validate(valid => {
        isValid = valid
      })
      return isValid
    },
    // 编辑
    async onEditSubmit() {
      this.editLoading = true
      const para = _.cloneDeep(this.editForm)

      const res = await dictionaryApi.update(para)
      this.editLoading = false

      if (!res?.success) {
        return
      }

      this.$message({
        message: this.$t('admin.updateOk'),
        type: 'success'
      })
      this.$refs['editForm'].resetFields()
      this.editFormVisible = false
      this.getDataList()
    },
    // 删除验证
    deleteValidate(row) {
      let isValid = true
      if (row && row.name === 'admin') {
        this.$message({
          message: row.description + '，禁止删除！',
          type: 'warning'
        })
        isValid = false
      }

      return isValid
    },
    // 删除
    async onDelete(index, row) {
      row._loading = true
      const para = { id: row.id }
      const res = await dictionaryApi.softDelete(para)
      row._loading = false

      if (!res?.success) {
        return
      }

      this.$message({
        message: this.$t('admin.deleteOk'),
        type: 'success'
      })

      this.getDataList()
    },
    // 批量删除验证
    batchDeleteValidate() {
      let isValid = true
      var row = this.sels && this.sels.find(s => s.name === 'admin')
      if (row && row.name === 'admin') {
        this.$message({
          message: row.description + '，禁止删除！',
          type: 'warning'
        })
        isValid = false
      }

      return isValid
    },
    // 批量删除
    async onBatchDelete() {
      const para = { ids: [] }
      para.ids = this.sels.map(s => {
        return s.id
      })

      this.deleteLoading = true
      const res = await dictionaryApi.batchSoftDelete(para.ids)
      this.deleteLoading = false

      if (!res?.success) {
        return
      }
      this.$message({
        message: this.$t('admin.batchDeleteOk'),
        type: 'success'
      })

      this.getDataList()
    },
    onSelectionChange: function(sels) {
      this.sels = sels
    }
  }
}
</script>
